/**
 * Copyright 2010 Creative Works, Inc.
 * Creative Works licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.sendvox.freswitch.core.impl;

import java.util.ArrayList;
import java.util.List;

import org.jboss.netty.channel.Channel;
import org.sendvox.freswitch.core.Application;
import org.sendvox.freswitch.core.SendMsg;
import org.sendvox.freswitch.outbound.OutboundClient;
import org.sendvox.freswitch.util.Arguments;

/**
 * <p>
 * Send a message to the call of given uuid (call-command execute or hangup).
 * Send message are used to send messages that controls the behavior of
 * FreeSWITCH. You need to provide a uid (the unique id for each call/session).
 * </p>
 * <p>
 * In order to control calls using send message, the calls should be sent to the
 * <code>mod_park</code> which just leaves the channel sitting in limbo,
 * allowing you to execute applications on the channel.
 * </p>
 * <p>
 * Usage: <code>sendmsg &lt;uuid&gt; </code>
 * </p>
 * <p>
 * Reference: <a
 * href="http://wiki.freeswitch.org/wiki/Event_Socket#sendmsg">http
 * ://wiki.freeswitch.org/wiki/Event_Socket#sendmsg</a>
 * </p>
 * 
 */
public final class SendMsgImpl implements SendMsg {

	private static final String SEND_MSG = "sendmsg";
	private static final String CALL_COMMAND = "call-command:";
	private static final String EXECUTE_APP_NAME = "execute-app-name:";
	private static final String EXECUTE_APP_ARG = "execute-app-arg:";
	private static final String LOOPS = "loops:";
	private static final String HANGUP_CAUSE = "hangup-cause:";
	private static final String EVENT_LOCK = "event-lock:";
	private static final String NOMEDIA_UUID = "nomedia-uuid:";

	private List<String> command;

	private static final String SPACE = " ";

	private String uuid;
	private CallCommand callCommand;
	private String executeAppName;
	private String executeAppArg;
	private Integer loops;
	private String hangupCause;
	private boolean eventLock;
	private String noMediaInfo;

	private OutboundClient outboundClient;

	SendMsgImpl() {
		command = new ArrayList<String>();
		command.add(SEND_MSG.concat(SPACE));
	}

	public SendMsgImpl(OutboundClient outboundClient) {
		this();
		this.outboundClient = outboundClient;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#uuid(java.lang.String)
	 */
	@Override
	public SendMsg uuid(String uuid) {
		Arguments.INSTANCE.validate(uuid).isNotNull().isNotEmpty();
		this.uuid = uuid;
		command.clear();
		command.add(SEND_MSG.concat(SPACE).concat(uuid.trim()));
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sendvox.freswitch.core.impl.SendMsg#callCommand(org.sendvox.freswitch
	 * .core.impl.SendMsgImpl.CallCommand)
	 */
	@Override
	public SendMsg callCommand(CallCommand callCommand) {
		command.clear();
		command.add(SEND_MSG.concat(SPACE));
		Arguments.INSTANCE.validate(callCommand).isNotNull();
		this.callCommand = callCommand;
		command.add(CALL_COMMAND.concat(SPACE).concat(callCommand.getLiteral()));
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sendvox.freswitch.core.impl.SendMsg#executeAppName(java.lang.String)
	 */
	@Override
	public SendMsg executeAppName(String executeAppName) {
		Arguments.INSTANCE.validate(callCommand, executeAppName).isNotNull()
				.isNotEmpty();
		this.executeAppName = executeAppName;
		command.add(EXECUTE_APP_NAME.concat(SPACE).concat(executeAppName));
		return this;
	}

	@Override
	public SendMsg executeAppName(Application executeAppName) {
		Arguments.INSTANCE.validate(callCommand, executeAppName).isNotNull();
		return this.executeAppName(executeAppName.getName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sendvox.freswitch.core.impl.SendMsg#executeAppArg(java.lang.String)
	 */
	@Override
	public SendMsg executeAppArg(String executeAppArg) {
		Arguments.INSTANCE.validate(callCommand, executeAppName, executeAppArg)
				.isNotNull().isNotEmpty();
		this.executeAppArg = executeAppArg;
		command.add(EXECUTE_APP_ARG.concat(SPACE).concat(executeAppArg));
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#loops(java.lang.Integer)
	 */
	@Override
	public SendMsg loops(Integer loops) {
		Arguments.INSTANCE
				.validate(callCommand, executeAppName, executeAppArg, loops)
				.isNotNull().isPositive();
		this.loops = loops;
		command.add(LOOPS.concat(SPACE).concat(loops.toString()));
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sendvox.freswitch.core.impl.SendMsg#hangupCause(java.lang.String)
	 */
	@Override
	public SendMsg hangupCause(String hangupCause) {
		Arguments.INSTANCE.validate(command, hangupCause).isNotNull()
				.isNotEmpty();
		if (!command.equals(CallCommand.HANGUP)) {
			throw new IllegalArgumentException("Command must be of HANGUP type");
		}
		this.hangupCause = hangupCause;
		command.add(HANGUP_CAUSE.concat(SPACE).concat(hangupCause));
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#eventLock(boolean)
	 */
	@Override
	public SendMsg eventLock(boolean eventLock) {
		this.eventLock = eventLock;
		command.add(EVENT_LOCK.concat(SPACE)
				.concat(Boolean.toString(eventLock)));
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#nomedia(java.lang.String)
	 */
	@Override
	public SendMsg nomedia(String noinfo) {
		Arguments.INSTANCE.validate(command, noinfo).isNotNull().isNotEmpty();
		if (!command.equals(CallCommand.NOMEDIA)) {
			throw new IllegalArgumentException(
					"Command must be of NOMEDIA type");
		}
		this.noMediaInfo = noinfo;
		command.add(NOMEDIA_UUID.concat(SPACE).concat(noinfo));
		return this;
	}

	public static final String LINE_TERMINATOR = "\n";

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		Arguments.INSTANCE.validate(command).isNotNull().isNotEmpty();
		for (String commandLine : command) {
			builder.append(commandLine).append(LINE_TERMINATOR);
		}
		builder.append(LINE_TERMINATOR);
		return builder.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getCommand()
	 */
	@Override
	public List<String> getCommand() {
		return command;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getUuid()
	 */
	@Override
	public String getUuid() {
		return uuid;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getCallCommand()
	 */
	@Override
	public CallCommand getCallCommand() {
		return callCommand;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getExecuteAppName()
	 */
	@Override
	public String getExecuteAppName() {
		return executeAppName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getExecuteAppArg()
	 */
	@Override
	public String getExecuteAppArg() {
		return executeAppArg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getLoops()
	 */
	@Override
	public Integer getLoops() {
		return loops;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getHangupCause()
	 */
	@Override
	public String getHangupCause() {
		return hangupCause;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#isEventLock()
	 */
	@Override
	public boolean isEventLock() {
		return eventLock;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.SendMsg#getNoMediaInfo()
	 */
	@Override
	public String getNoMediaInfo() {
		return noMediaInfo;
	}

	@Override
	public SendMsg execute(Channel channel) {
		outboundClient.executeSyncMultiLineCommand(channel, getCommand());
		return this;
	}

}
